﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

/*
 * Opisuje prymityw przedstawiający okrąg.
 * Domyślnie znajduje się on na płaszczyźnie wyznaczonej przez osie XY,
 * jednak po zastosowaniu rotacji można go przenieść w dowolne miejsce.
 */
namespace Projekt.Models.Primitives
{
    public class Circle : DrawableGameComponent
    {
        Engine game;

        VertexPositionColor[] vertices;
        private GraphicsDevice graphicsDevice;
        BasicEffect basicEffect;
        private Vector3 position;
        public Vector3 Position
        {
            get { return position; }
            set
            {
                position = value;
                Transform();
            }
        }
        private Vector3 rotation;
        public Vector3 Rotation
        {
            get { return rotation; }
            set
            {
                rotation = value;
                Transform();
            }
        }
        private Vector3 radius;
        public Vector3 Radius
        {
            get { return radius; }
            set
            {
                radius = value;
                Transform();
            }
        }
        Matrix Transforms;

        /// <summary>
        /// Tworzy okrąg o podanych parametrach
        /// </summary>
        /// <param name="Position">Punkt środkowy okręgu</param>
        /// <param name="Rotation">Wektor obrotu</param>
        /// <param name="Radius">Promień wyrażony jako wektor, [w poziomie, w pionie, brak]</param>
        /// <param name="color">Kolor</param>
        /// <param name="graphicsDevice"></param>
        public Circle(Vector3 Position, Vector3 Rotation, Vector3 Radius, Color color, GraphicsDevice graphicsDevice, Engine game)
            : base(game)
        {
            this.graphicsDevice = graphicsDevice;
            this.Position = Position;
            this.Rotation = Rotation;
            this.Radius = Radius;
            Transforms = Matrix.Identity;
            this.game = game;
            this.game.Components.Add(this);

            vertices = new VertexPositionColor[100];
            for (int i = 0; i < 100; i++)
            {
                float angle = (float)(i / 100.0 * Math.PI * 2);
                vertices[i].Position = new Vector3((float)Math.Cos(angle), (float)Math.Sin(angle), 0);
                vertices[i].Color = color;
            }
            vertices[99] = vertices[0];

            basicEffect = new BasicEffect(graphicsDevice);
            basicEffect.VertexColorEnabled = true;

            Transform();
        }

        void Transform()
        {
            Transforms = Matrix.CreateScale(Radius) * Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z) * Matrix.CreateTranslation(Position);
        }

        public override void Draw(GameTime gameTime)
        {
            basicEffect.Projection = game.camera.Projection;
            basicEffect.View = game.camera.View;
            basicEffect.World = Transforms;

            basicEffect.CurrentTechnique.Passes[0].Apply();

            graphicsDevice.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, 99);
            
            base.Draw(gameTime);
        }
    }
}
